//
//  SinaOAuthViewController.m
//  MySinaWeiboProjectForOC
//
//  Created by apple on 2016/11/16.
//  Copyright © 2016年 WangChen. All rights reserved.
//

#import "SinaOAuthViewController.h"
#import <SVProgressHUD.h>
#import "UIBarButtonItem+extension.h"
#import "SinaUserAccountViewModel.h"

@interface SinaOAuthViewController () <UIWebViewDelegate>
@property (nonatomic,strong) UIWebView *webView;
@end

@implementation SinaOAuthViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupUI];
    
    [self loadRequest];
}

#pragma mark
#pragma mark - 布置界面
-(void)setupUI{
    
    //返回按钮
    UIBarButtonItem *backBtnItem = [[UIBarButtonItem alloc]initWithTitle:@"返回" image:@"navigationbar_back_withtext" addTarget:self action:@selector(backClick)];
    
    self.navigationItem.leftBarButtonItem = backBtnItem;
    
    //自动填充按钮
    UIBarButtonItem *autoPutInBtn = [[UIBarButtonItem alloc]initWithTitle:@"自动填充" image:nil addTarget:self action:@selector(autoBtnClick)];
    
    self.navigationItem.rightBarButtonItem = autoPutInBtn;
    
    
}

#pragma mark
#pragma mark - 点击自动填充
-(void)autoBtnClick{
    
    NSString *jsString = @"document.getElementById('userId').value = 'qhwc2009@sina.cn', document.getElementById('passwd').value = 'wangchen138714'";
    [self.webView stringByEvaluatingJavaScriptFromString:jsString];
    
}



#pragma mark
#pragma mark - 点击返回
-(void)backClick{
    [self dismissViewControllerAnimated:YES completion:nil];
}




#pragma mark
#pragma mark - webView加载请求
-(void)loadRequest{
    NSString *urlStr = [NSString stringWithFormat:@"%@?client_id=%@&redirect_uri=%@",authorizeURLString,appKey,redirectUrlStr];
    NSLog(@"%@",urlStr);
    NSURL *url = [NSURL URLWithString:urlStr];
    
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    
    [self.webView loadRequest:request];
}



#pragma mark
#pragma mark - 懒加载webView
-(UIWebView *)webView{
    if (!_webView) {
        _webView = [[UIWebView alloc]init];
        _webView.delegate = self;
    }
    return _webView;
}



#pragma mark
#pragma mark - 将根视图转变成webView
-(void)loadView{
    self.view = self.webView;
    
}


#pragma mark
#pragma mark - 协议实现方法
-(void)webViewDidStartLoad:(UIWebView *)webView{
    
    [SVProgressHUD show];
}

-(void)webViewDidFinishLoad:(UIWebView *)webView{
    
    [SVProgressHUD dismiss];
}


-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
    
    NSString *urlStr = [request.URL absoluteString];
    NSString *flag = @"code=";
    if([urlStr containsString:flag]){
        
        NSString *query = [request.URL query];
        NSString *code = [query substringFromIndex:flag.length];
        //访问token
        [[SinaUserAccountViewModel shared] accessTheTokenWithCode:code finishBlock:^(BOOL res) {
            if(!res){
                [SVProgressHUD showErrorWithStatus:@"世界上最远的距离就是没有网络"];
                return;
            }
            //成功就跳转欢迎界面
            [[NSNotificationCenter defaultCenter]postNotificationName:kNotificationChangeVC object:kNotificationToWelcome];
            [SVProgressHUD dismiss];
            [self dismissViewControllerAnimated:YES completion:nil];
        }];
        return false;
    }
    
    
    return YES;
}


@end
